<?php
namespace App\Common\Cla\Com\Token;

use App\Common as com;

class Data extends TokenBase {
	use TraitKeyDbOperate;

	/**
	 * 获取Token->Session缓存键对象
	 * @return com\Lib\Cache\KeyDB
	 */
	public function keyDbObj(  ) {
		if( $this->_keyDbObj === null ){
			$this->_keyDbObj = com\Lib\Cache\KeyDB::use( 'reb.open.v1.api.token' )->param( $this->tokenObj->getAccessToken() );
		}
		return $this->_keyDbObj;
	}

	/**
	 * 读取数据id
	 * @return string 数据id
	 */
	public function getDataId(  ):string {
		$data = $this->getOfPath( 'dataId', null );
		if( $data === null ) throw com\Lib\Err\Exception::makeOfNew( 'token数据id不存在，请重新申请token', com\Lib\Err\Code::$sysDataError );
		return $data;
	}

	/**
	 * @param int $expire
	 */
	public function refreshTtl( int $expire ) {
		$data = $this->get();
		// 数据不存在则不刷新
		if( $data !== false ){
			// 更新 expireTime
			$data['expireTime'] = com\Lib\Fun\Time::getTime() + $expire;
			//
			$this->set( $data, $expire );
		}
	}

	/**
	 * 读取数据剩余时间
	 * @return int|null [null]永久 [int]剩余时间戳
	 */
	public function getSupTime(  ):?int {
		$ttlOrNull = $this->keyDbObj()->ttlOrFail();
		//
		if( $ttlOrNull === null ){
			return null;
		}else{
			return $ttlOrNull;
		}

	}

}
